#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pb push_back
#define y1 y_alpha_1

using namespace std;

using ll = long long;
using db = double;
using vec = vector<int>;
using pii = pair<int,int>;

int read() {
	int x = 0; bool f = 0; char ch = getchar();
	while(!isdigit(ch)) f |= ch == '-',ch = getchar();
	while(isdigit(ch)) x = (x << 1) + (x << 3) + (ch ^ 48),ch = getchar();
	return f ? -x : x;
}

mt19937 rnd(19491001);

const int N = 5e5 + 10;
const int inf = 2147483647;
int n;

namespace treap {
	const int V = 1e9 + 7;
	int pri[N << 6],ch[N << 6][2],val[N << 6],ndn,siz[N << 6],rt[N];
	int a,b,c,d;
	int newnode(int x) { pri[++ndn] = rnd() % V; ch[ndn][0] = ch[ndn][1] = 0; val[ndn] = x; siz[ndn] = 1; return ndn; }
	int copy(int x) { pri[++ndn] = rnd() % V; ch[ndn][0] = ch[x][0]; ch[ndn][1] = ch[x][1]; val[ndn] = val[x]; siz[ndn] = siz[x]; return ndn; }
	void pushup(int x) { siz[x] = siz[ch[x][0]] + siz[ch[x][1]] + 1; } 
	void split(int cur,int k,int &x,int &y) {
		if(!cur) return x = y = 0, void();
		if(val[cur] <= k) x = copy(cur),split(ch[cur][1],k,ch[x][1],y),pushup(x);
		else y = copy(cur),split(ch[cur][0],k,x,ch[y][0]),pushup(y);
	}
	int merge(int x,int y) {
		if(!x || !y) return x | y; int z;
		if(pri[x] < pri[y]) return z = copy(x),ch[z][1] = merge(ch[z][1],y),pushup(z),z;
		else return z = copy(y),ch[z][0] = merge(x,ch[z][0]),pushup(z),z;
	}
	int k_th(int x,int k) {
		while(true) {
			if(siz[ch[x][0]] + 1 == k) return x;
			if(k > siz[ch[x][0]] + 1) k -= siz[ch[x][0]] + 1,x = ch[x][1];
			else x = ch[x][0];
		} 
	}
	void insert(int &rt,int x) { split(rt,x,a,b); rt = merge(merge(a,newnode(x)),b); }
	void del(int &rt,int x) { split(rt,x,a,b); split(a,x - 1,c,d); if(val[d] == x) d = merge(ch[d][0],ch[d][1]); rt = merge(merge(c,d),b); }
	int getrank(int &rt,int x) { split(rt,x - 1,a,b); int ans = siz[a] + 1; rt = merge(a,b); return ans; }
	int prev(int &rt,int x) { split(rt,x - 1,a,b); if(!siz[a]) return rt = merge(a,b),-inf; int ans = val[k_th(a,siz[a])]; rt = merge(a,b); return ans; }
	int nxtv(int &rt,int x) { split(rt,x,a,b); if(!siz[b]) return rt = merge(a,b),inf; int ans = val[k_th(b,1)]; rt = merge(a,b); return ans; }
}

using treap :: rt;

int main() {
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	n = in; 
	for(int i = 1;i <= n;i++) {
		int v = in,op = in,x = in; rt[i] = rt[v];
		if(op == 1) treap :: insert(rt[i],x);
		if(op == 2) treap :: del(rt[i],x);
		if(op == 3) printf("%d\n",treap :: getrank(rt[i],x));
		if(op == 4) printf("%d\n",treap :: val[treap :: k_th(rt[i],x)]);
		if(op == 5) printf("%d\n",treap :: prev(rt[i],x));
		if(op == 6) printf("%d\n",treap :: nxtv(rt[i],x));
	}
	return 0;
}
